package com.myna.upload.file.upload;


import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import static com.myna.csv.initial.InitialProcessor.fileUploadPath;
import static com.myna.csv.initial.InitialProcessor.rdcFilePath;

public class UploadFileProcesser {

    public static final Logger LOGGER = LogManager.getLogger(UploadFileProcesser.class);

    List<File> filePathList = new ArrayList();
    int index = 0;

    public UploadFileProcesser() {
        File file = new File(rdcFilePath);
        getFileList(file);
    }

    public void getFileList(File file) {
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isDirectory()) {
                getFileList(file1);
            } else if (file1.isFile() && file1.getName().lastIndexOf(".zip") != -1) {
                filePathList.add(file1);
            }
        }
    }

    public void process() {
        File file = null;
        Long startTime = System.currentTimeMillis();

        while (index < filePathList.size()) {
            file = filePathList.get(index);
            index++;
            Long one_startTime = System.currentTimeMillis();

            zipFile(file);

            Long one_endTime = System.currentTimeMillis();
            LOGGER.info("one file upload cost time :" + (one_endTime - one_startTime));
        }

        Long endTime = System.currentTimeMillis();
        LOGGER.info("upload All Files cost Time ： " + (endTime - startTime));
    }

    private void upload(File file) {
        FileOutputStream out = null;
        ZipInputStream in = null;
        try {
            in = new ZipInputStream(new FileInputStream(file));
            while (in.getNextEntry() != null) {
                File f = new File(fileUploadPath + in.getNextEntry().getName());
                if (!f.exists()) {
                    f.createNewFile();
                }
                out = new FileOutputStream(f);
                int b;
                while ((b = in.read()) != -1) {
                    out.write(b);
                }
                out.close();
            }
        } catch (Exception e) {
            LOGGER.info("File upload error", e);
        } finally {
            if (!Objects.isNull(out)) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (!Objects.isNull(in)) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void zipFile(File file) {
        //定义压缩文件的名称
        ZipInputStream in = null;

        //定义输入文件流
        InputStream input = null;
        ZipOutputStream zipOut = null;
        try {
            in = new ZipInputStream(new FileInputStream(file), Charset.forName("UTF-8"));
            ZipEntry zipEntry = in.getNextEntry();
            String zipFileName = zipEntry.getName().replace(".csv", ".zip");
            File zipFile = new File(fileUploadPath + zipFileName);
            if (!zipFile.exists()) {
                zipFile.createNewFile();
            }
            zipOut = new ZipOutputStream(new FileOutputStream(zipFile), Charset.forName("UTF-8"));
            zipOut.putNextEntry(new ZipEntry(zipEntry.getName()));
            int temp = 0;
            while ((temp = in.read()) != -1) {
                zipOut.write(temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (!Objects.isNull(in)) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (!Objects.isNull(zipOut)) {
                try {
                    zipOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}